package com.example.jdcompetition.Service.cvsPro.jd;


import com.example.jdcompetition.Component.CsvPaths;
import com.example.jdcompetition.Controller.UniformReponseHandler;
import com.example.jdcompetition.Service.cvsPro.dataprocess.CsvProcess;
import com.example.jdcompetition.Entity.CallResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.file.Paths;

import static com.example.jdcompetition.JdcompetitionApplication.*;


/**
 * @author henrydai
 * @date 2021/6/4 20:27
 */
@Service
public class SaveData {
    @Autowired
    private CsvProcess csvProcess;
    //******************end:文件静态参数区***************************************
    public CallResultMsg saveAll(CsvPaths paths)
    {
        long start = System.currentTimeMillis();
        //简单同步,处理数据时不进行查询
        if(!isPrepared)
        {
            collect.clear();
            try {
                saveCollect(paths);
            } catch (Exception e) {
                e.printStackTrace();
                isPrepared=false;
                return new UniformReponseHandler<String>().collectFileFailed("read collection files failed!");
            }
            transit.clear();

            try {
                saveTransit(paths);
            } catch (Exception e) {
                e.printStackTrace();
                isPrepared=false;
                return new UniformReponseHandler<String>().transitFileFailed("read transit files failed!");
            }
            deliver.clear();

            try {
                saveDeliver(paths);
            } catch (Exception e) {
                e.printStackTrace();
                isPrepared=false;
                return new UniformReponseHandler<String>().deliverFileFailed("read deliver files failed!");
            }
            isPrepared=true;
            return new UniformReponseHandler<String>().sendSuccessResponse("运行:"+(System.currentTimeMillis()-start)+"ms");
        }
        else
        {
            return new UniformReponseHandler<String>().unkownException("文件已经处理完毕,无须再处理");
        }
    }


    public void saveCollect(CsvPaths paths) throws Exception {
        //*******************************处理揽收文件***********************
        csvProcess.solveCollect(Paths.get(paths.getFilePath(),paths.getCollectFile1()),paths.getCharsetName1());
        csvProcess.solveCollect(Paths.get(paths.getFilePath(),paths.getCollectFile2()),paths.getCharsetName1());
        csvProcess.solveCollect(Paths.get(paths.getFilePath(),paths.getCollectFile3()),paths.getCharsetName1());
        //*******************************处理揽收文件***********************

    }

    public void saveTransit(CsvPaths paths) throws Exception {
        //*******************************中转文件****************************
        csvProcess.solveTransit(Paths.get(paths.getFilePath(),paths.getTransitFile1()),paths.getCharsetName1());
        csvProcess.solveTransit(Paths.get(paths.getFilePath(),paths.getTransitFile2()),paths.getCharsetName1());
        csvProcess.solveTransit(Paths.get(paths.getFilePath(),paths.getTransitFile3()),paths.getCharsetName1());
        //*******************************中转文件****************************
    }

    public void saveDeliver(CsvPaths paths) throws Exception {
        //*******************************派送文件****************************
        csvProcess.solveDeliver(Paths.get(paths.getFilePath(),paths.getDeliverFile1()),paths.getCharsetName1());
        csvProcess.solveDeliver(Paths.get(paths.getFilePath(),paths.getDeliverFile2()),paths.getCharsetName1());
        csvProcess.solveDeliver(Paths.get(paths.getFilePath(),paths.getDeliverFile3()),paths.getCharsetName1());
        //*******************************派送文件****************************
    }

}
